<template>
  <div class="app-container">
    <!-- 添加或修改保险合同佣金支付对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="计划批次" prop="planId">
          <el-select v-model="form.planId" placeholder="请选择投保人名称" @change="planChange">
            <el-option
                v-for="dict in pcList"
                :key="dict.id"
                :label="dict.planName"
                :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <!--<el-form-item label="批次名称" prop="realName">-->
        <!--  <el-input v-model="form.realName" placeholder="请输入批次名称" />-->
        <!--</el-form-item>-->
        <el-form-item label="实际支付金额" prop="realAmount">
          <el-input v-model="form.realAmount" placeholder="请输入实际支付金额">
            <template slot="append">
              <el-button slot="append">元</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="实际支付时间" prop="realDate">
          <el-date-picker clearable
                          v-model="form.realDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择实际支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付比例" prop="rate">
          <el-input v-model="form.rate" placeholder="请输入支付比例">
            <template slot="append">
              <el-button slot="append">%</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="支付责任人" prop="responsiblePerson">
          <el-input v-model="form.responsiblePerson" placeholder="请输入支付责任人"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listReal, getReal, delReal, addReal, updateReal} from "@/api/commission/real";
import {isEmpty, isValidAmount, isValidDecimal} from "@/utils/validate";
import {listPlan} from "@/api/commission/plan";

export default {
  name: "commissionReal",
  props: {
    insureId: {
      type: String || Number,
      default: ''
    },
    contractId: {
      type: String || Number,
      default: ''
    }
  },
  data() {

    const checkPlanAmountRel = (rule, value, callback) => {
      if (isEmpty(value)) {
        callback();
        return
      }
      if (!isValidAmount(value)) {
        callback(new Error('请输入正确的金额'));
        return;
      }
      callback();
    }
    const checkRateRel = (rule, value, callback) => {
      if (isEmpty(value)) {
        callback();
        return
      }

      if (value > 100) {
        callback(new Error('回款比例不能大于100'));
        return;
      }
      if (value < 0) {
        callback(new Error('回款比例不能小于0'));
        return;
      }
      if (!isValidDecimal(value)) {
        callback(new Error('请输入正确的比例，小数不能超过2位'));
        return;
      }
      callback();
    }
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 保险合同佣金支付表格数据
      realList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        insureId: null,
        contractId: null,
        planId: null,
        realName: null,
        realAmount: null,
        realDate: null,
        rate: null,
        responsiblePerson: null,
      },
      // 表单参数
      form: {},
      pcList: [],
      // 表单校验
      rules: {
        planId:[
          {required: true, message: '计划批次不能为空', trigger: 'blur'}
        ],
        realAmount: [
          {validator: checkPlanAmountRel, trigger: 'blur'}
        ],
        rate: [
          {validator: checkRateRel, trigger: 'blur'}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getPcList() {
      const queryParams = {}
      queryParams.contractId = this.contractId
      queryParams.insureId = this.insureId
      queryParams.pageSize = 9999
      queryParams.pageNum = 1
      listPlan(queryParams).then(response => {
        this.pcList = response.rows;
      });
    },
    planChange(row) {
      let pc = this.pcList.find(item => item.id == row)
      this.form.realName = pc.planName
      this.form.planName = pc.planName
    },
    openForm(id) {
      this.getPcList()
      if (isEmpty(id)) {
        this.handleAdd()
        return
      }
      this.handleUpdate(id)
    },
    /** 查询保险合同佣金支付列表 */
    getList() {
      this.$parent.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        insureId: null,
        contractId: null,
        planId: null,
        realName: null,
        realAmount: null,
        realDate: null,
        rate: null,
        responsiblePerson: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加保险合同佣金支付";
    },
    /** 修改按钮操作 */
    handleUpdate(id) {
      this.reset();
      getReal(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改保险合同佣金支付";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.contractId = this.contractId
          this.form.insureId = this.insureId
          if (this.form.id != null) {
            updateReal(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReal(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除保险合同佣金支付编号为"' + ids + '"的数据项？').then(function () {
        return delReal(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('commission/real/export', {
        ...this.queryParams
      }, `real_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
